package com.yc.damai.web.servlet;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.yc.damai.bean.Result;
import com.yc.damai.web.BaseServlet;

@WebServlet("/upload.s")
@MultipartConfig
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Part part = request.getPart("file");
		
		// http://localhost/C94-S2-Ply-damai  项目根目录
		// http://localhost  服务器根目录 
	    // http://localhost/a.jpg  webapps/ROOT目录  a.jpg
		
		String realpath = this.getServletContext().getRealPath("/"); // C94-S2-Ply-damai
		File file = new File(realpath);
		
		realpath = file.getParentFile().getParentFile().getAbsolutePath();
		
		realpath += "/webapps/ROOT";
		
		String filename = part.getSubmittedFileName();
		String filepath = realpath + "/" + filename;
		String webpath = "/" + filename;
		
		part.write(filepath);
		
		Result res = new Result(1,"文件上传成功", webpath);
		
		BaseServlet.toJson(response, res);
		
		
	}

}
